<template>
  <div>
    <!-- 三级分类 -->
    <Category :scene="scene"></Category>
    <!-- 卡片 -->
    <el-card shadow="always" style="margin: 10px 0">
      <!-- v-if会重新销毁创建，频繁切换不推荐 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          size="default"
          @click="scene = 1"
          icon="Plus"
          :disabled="!categoryStore.c3Id"
        >
          添加SPU
        </el-button>
        <!-- 展示已有spu数据 -->
        <el-table :data="records" style="margin: 10px 0" border>
          <el-table-column
            label="序号"
            width="100"
            type="index"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="SPU操作">
            <template #>
              <!-- row:即为已有的spu对象 -->
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
              ></el-button>
              <el-button
                type="warning"
                size="small"
                icon="Edit"
                title="修改SPU"
                @click="scene = 1"
              ></el-button>
              <el-button
                type="info"
                size="small"
                icon="InfoFilled"
                title="查看SKU列表"
              ></el-button>
              <el-button
                type="danger"
                size="small"
                icon="Delete"
                title="删除SPU"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <div class="demo-pagination-block" style="margin-top: 15px">
          <el-pagination
            v-model:current-page="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[3, 5, 7, 9]"
            :background="true"
            layout=" prev, pager, next, jumper, -> ,sizes,total"
            :total="total"
            @change="getSpu"
            @size-change="handleSizeChange"
          />
        </div>
      </div>
      <!-- 添加SPU|修改SPU子组件 -->
      <SpuForm v-show="scene == 1" @changeScene="changeScene"></SpuForm>
      <!-- 添加SKU子组件 -->
      <SkuForm v-show="scene == 2"></SkuForm>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'
//引入目录分类的仓库
import useCategoryStore from '../../../store/modules/category'
//引入api
import { reqHasSpu } from '../../../api/product/spu'
//引入ts类型
import type { SpuResponseData, Records } from '../../../api/product/spu/type'
//引入子组件
import SpuForm from './spuForm.vue'
import SkuForm from './skuForm.vue'

//获取仓库实例
const categoryStore = useCategoryStore()

//场景值
//0:展示spu列表数据;1:添加|修改spu;2:添加sku
let scene = ref<number>(0)
//分页---当前页码
const currentPage = ref<number>(1)
//每页显示条数
const pageSize = ref<number>(3)
//存储已有spu数据总数
let total = ref<number>(0)
//spu数据
let records = ref<Records>([])

//监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    getSpu()
  },
)
//发请求获取某一个三级分类下全部的spu
const getSpu = async () => {
  //保证c3Id有值
  if (!categoryStore.c3Id) return
  //发请求
  let result: SpuResponseData = await reqHasSpu(
    currentPage.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (result.code == 200) {
    total.value = result.data.total
    records.value = result.data.records
  }
}

const handleSizeChange = () => {
  //当前每一页数据量发生变化时，当前页码归一
  currentPage.value = 1
}

//子组件spuForm绑定的自定义事件，让子组件通知父组件切换场景
const changeScene = (value: number) => {
  scene.value = value
}
</script>

<style scoped></style>
